package com.wbpay.sdk;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;
import android.widget.Toast;

import com.wbpay.sdk.constant.Constants;
import com.wbpay.sdk.framework.IUserLogin;
import com.wbpay.sdk.framework.IUserPay;
import com.wbpay.sdk.framework.PayModule;
import com.wbpay.sdk.framework.UserModule;
import com.wbpay.sdk.listener.OnUserExchangeListener;
import com.wbpay.sdk.listener.OnUserLoginListener;
import com.wbpay.sdk.listener.OnUserLogoutListener;
import com.wbpay.sdk.listener.PayCallback;
import com.wbpay.sdk.model.SdkInfo;
import com.wbpay.sdk.params.PayParams;
import com.wbpay.sdk.utils.Utils;

/**
 * sdk的接口类
 */
public class WbSdk {

    private static IUserLogin mUserLogin;
    private static IUserPay mUserPay;
    private static Handler mHandler = null;
    private static volatile boolean mInited = false;
    private static Activity mActivity = null;

    private static void checkActivityContext(Context context) {
        if (!(context instanceof Activity)) {
            throw new IllegalArgumentException("Context must be an Activity");
        }
    }

    public static void init(Activity activity, String appId) {
        String channel = Utils.getMetaData(activity, Constants.CUSTOM_CHANNEL);
        if (TextUtils.isEmpty(channel) || channel.length() > 10) {
            throw new IllegalArgumentException("custom_channel config error");
        }
        init(activity, appId, channel);
    }

    public static void init(Activity activity) {
        init(activity, null, null);
    }

    public static void init(Activity activity, String appId, String channel) {
        checkActivityContext(activity);
        if (!mInited) {
            SdkInfo.get(activity).init(appId, channel);
            initLoginAndPay(activity);
            mInited = true;
        }
    }

    private static void initLoginAndPay(Activity activity) {
        mActivity = activity;
        mUserLogin = new UserModule(activity);
        mUserPay = new PayModule(activity);
    }

    /**
     * 登录接口
     *
     * @param l
     */
    public static void login(Activity activity, OnUserLoginListener l) {
        checkActivityContext(activity);
        if (mActivity != null && mActivity != activity) {
            initLoginAndPay(activity);
        }
        if (mUserLogin == null) {
            showToast(activity, Utils.getStringId(activity, "jy_sdk_not_init"));
            return;
        }
        mUserLogin.login(l);
    }

    /**
     * 切换账户
     *
     * @param l
     */
    public static void exchange(Activity activity, OnUserExchangeListener l) {
        checkActivityContext(activity);
        if (mActivity != null && mActivity != activity) {
            initLoginAndPay(activity);
        }
        if (mUserLogin == null) {
            showToast(activity, Utils.getStringId(activity, "jy_sdk_not_init"));
            return;
        }
        mUserLogin.exchange(l);
    }

    /**
     * 支付
     *
     * @param payParams
     * @param callback
     */
    public static void pay(Activity activity, PayParams payParams, final PayCallback callback) {
        checkActivityContext(activity);
        if (mActivity != null && mActivity != activity) {
            initLoginAndPay(activity);
        }
        if (mUserPay == null) {
            showToast(activity, Utils.getStringId(activity, "jy_sdk_not_init"));
            return;
        }
        mUserPay.pay(payParams, callback);
    }

    private static void showToast(final Activity activity, final String msg) {
        if (mHandler == null) {
            mHandler = new Handler();
        }
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(activity, msg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    private static void showToast(final Activity activity, final int msgId) {
        showToast(activity, activity.getResources().getString(msgId));
    }

    public static void logout(Activity activity, OnUserLogoutListener l) {
        checkActivityContext(activity);
        if (mActivity != null && mActivity != activity) {
            initLoginAndPay(activity);
        }
        if (mUserLogin == null) {
            showToast(activity, Utils.getStringId(activity, "jy_sdk_not_init"));
            return;
        }
        mUserLogin.logout(l);
    }

    public static void exit(Activity activity) {
        exit(activity, null);
    }

    public static void exit(Activity activity, final OnExitCallback callback) {
        checkActivityContext(activity);
        if (mActivity != null && mActivity != activity) {
            initLoginAndPay(activity);
        }
        if (mUserLogin == null) {
            showToast(activity, Utils.getStringId(activity, "jy_sdk_not_init"));
            return;
        }
        if (callback != null) {
            mUserLogin.exit(new OnExitCallback() {
                @Override
                public void onExitConfirm() {
                    mUserLogin = null;
                    mUserPay = null;
                    mInited = false;
                    mActivity = null;
                    callback.onExitConfirm();
                }

                @Override
                public void onExitCancel() {
                    callback.onExitCancel();
                }
            });
        } else {
            mUserLogin = null;
            mUserPay = null;
            mInited = false;
            mActivity = null;
        }
    }

    public interface OnExitCallback {
        public void onExitConfirm();
        public void onExitCancel();
    }
}
